﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Actuals.CoreDomain.Business;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;
using Actuals.Data;

namespace Actuals.Business
{
    public class BusinessEmployeeLeave : IBusinessEmployeeLeave
    {


        private IDalEmployeeLeave _dalEmployeeLeave;

        public BusinessEmployeeLeave(IDalEmployeeLeave dalEmployeeLeave)
        {
            _dalEmployeeLeave = dalEmployeeLeave;
        }

        public BusinessEmployeeLeave()
        {
            _dalEmployeeLeave = new DalEmployeeLeave();
        }


        #region IBusinessEmployeeLeave Members

        public IList<IEmployeeLeave> GetEmployeeLeaves(DateTime fromDate, DateTime toDate)
        {
            return _dalEmployeeLeave.GetEmployeeLeaves(fromDate, toDate);
        }

        public bool IsEmployeeOnLeave(int employeeId, DateTime date)
        {
            IBusinessEmployeeLeave businessEmployeeLeaves = new BusinessEmployeeLeave();
            IList<IEmployeeLeave> employeeLeaves = businessEmployeeLeaves.GetEmployeeLeaves(date, date);
            bool b = false;
            foreach (IEmployeeLeave employeeleave in employeeLeaves)
            {

                if (employeeleave.EmpId == employeeId)

                    b = true;
            }
            return b;
        }

        public void ValidateEmployeeLeave(IEmployeeLeave employeeLeave, IEmployeeLeaveUI employeeLeaveUI)
        {
            DateTime _fromdate = employeeLeaveUI.FromDate;
            DateTime _toDate = employeeLeaveUI.ToDate;
            ILeaveDates leaveDate = new LeaveDates();
            List<ILeaveDates> leaveDates = new List<ILeaveDates>();
            leaveDate.Date = _fromdate;
           
                leaveDate.AM = employeeLeaveUI.AM;
                if (leaveDate.AM)
                    leaveDate.PM = true;
                else
                    leaveDate.PM = employeeLeaveUI.PM;

                leaveDates.Add(leaveDate);




            DateTime fromDate = _fromdate.AddDays(1.0);
            DateTime toDate = _toDate.AddDays(-1.0);
            for (DateTime cd = fromDate; cd <= toDate; cd = cd.AddDays(1.0))
            {
                ILeaveDates leaveDate1 = new LeaveDates();
                leaveDate1.Date = cd;
                leaveDate1.AM = true;
                leaveDate1.PM = true;
                leaveDates.Add(leaveDate1);

            }

           
            ILeaveDates leaveDateExist = leaveDates.Find(_leaveDate => _leaveDate.Date == _toDate);
            if (leaveDateExist == null)
            {
                ILeaveDates leaveDate2 = new LeaveDates();
                leaveDate2.Date = _toDate;
                if (employeeLeaveUI.ToPM == true)
                {
                    leaveDate2.AM = true;
                    leaveDate2.PM = true;

                }
                else
                {
                    leaveDate2.AM = true;
                    leaveDate2.PM = false;
                }
                  leaveDates.Add(leaveDate2);
            }
            else
            {
                leaveDateExist.PM = employeeLeaveUI.ToPM;
               
            }


            IBusinessEmployeeLeave businessEmployeeLeave = new BusinessEmployeeLeave();
            foreach (ILeaveDates validDate in leaveDates)
            {
                bool b = (validDate.Date.DayOfWeek == DayOfWeek.Saturday) || (validDate.Date.DayOfWeek == DayOfWeek.Sunday) || (((new BusinessPublicHoliday()).IsPublicHoliday(validDate.Date)));

                if (b == false)
                {
                    employeeLeave.LeaveDate = validDate.Date;
                    employeeLeave.AM = validDate.AM;
                    employeeLeave.PM = validDate.PM;
                    employeeLeave.LeaveType = employeeLeaveUI.LeaveType;
                    businessEmployeeLeave.AddEmployeeLeave(employeeLeave);
                }
                
                
               
            }

        }


        /*int count = 0;
           
            for (int i = 0; i <= leaveDates.Count; i++)
            {

                
                if (leaveDates[i].Date == _toDate)
                {
                    count++;
                }
            }
            if (count == 0)
            {

            


            }
            */
        /*     public void  ValidateEmployeeLeave(IEmployeeLeave employeeLeave, IEmployeeLeaveUI employeeLeaveUI)
             {
                 IBusinessEmployeeLeave businessEmployeeLeave = new BusinessEmployeeLeave();
                 //for (DateTime date = employeeLeaveUI.FromDate; date <= employeeLeaveUI.ToDate; date = (date.AddDays(1.0)))
                 //{
                 //  bool b = (date.DayOfWeek == DayOfWeek.Saturday) || (date.DayOfWeek == DayOfWeek.Sunday) || (((new BusinessPublicHoliday()).IsPublicHoliday(date)));
                 DateTime _fromdate = employeeLeaveUI.FromDate;
                 DateTime _toDate = employeeLeaveUI.ToDate;
                 if (_fromdate != _toDate)
                 {
             
         * DateTime fromDate =_fromdate.AddDays(1.0);
                     DateTime toDate = _toDate.AddDays(-1.0);
                     for (DateTime cd = fromDate; cd <= toDate;  cd = cd.AddDays(1.0))
                     {
                         bool b = (cd.DayOfWeek == DayOfWeek.Saturday) && (cd.DayOfWeek == DayOfWeek.Sunday) && (((new BusinessPublicHoliday()).IsPublicHoliday(cd)));

                         if (b == false)
                         {
                             employeeLeave.LeaveDate = cd;
                             employeeLeave.AM = true;
                             employeeLeave.PM = true;
                             businessEmployeeLeave.AddEmployeeLeave(employeeLeave);

                         }
                     }

                     employeeLeave.LeaveDate = _fromdate;
                     bool b1 = (_fromdate.DayOfWeek == DayOfWeek.Saturday) || (_fromdate.DayOfWeek == DayOfWeek.Sunday) || (((new BusinessPublicHoliday()).IsPublicHoliday(_fromdate)));
                     if (employeeLeaveUI.AM == true && b1 == false)
                     {
                         employeeLeave.AM = true;
                         employeeLeave.PM = true;
                     }
                     else if (employeeLeaveUI.PM == true && b1 == false)
                     {
                         employeeLeave.AM = false;
                         employeeLeave.PM = true;
                     }
                     businessEmployeeLeave.AddEmployeeLeave(employeeLeave);

                     employeeLeave.LeaveDate = _toDate;
                     bool b2 = (_fromdate.DayOfWeek == DayOfWeek.Saturday) || (_fromdate.DayOfWeek == DayOfWeek.Sunday) || (((new BusinessPublicHoliday()).IsPublicHoliday(_fromdate)));
                     if (employeeLeaveUI.ToAM == true && b1 == false)
                     {
                         employeeLeave.AM = true;
                         employeeLeave.PM = false;
                     }
                     else if (employeeLeaveUI.ToPM == true && b1 == false)
                     {
                         employeeLeave.AM = true;
                         employeeLeave.PM = true;
                     }
                     businessEmployeeLeave.AddEmployeeLeave(employeeLeave);

                 }

                 else if (_fromdate == _toDate)
                 {

                     bool b = (_fromdate.DayOfWeek == DayOfWeek.Saturday) || (_fromdate.DayOfWeek == DayOfWeek.Sunday) || (((new BusinessPublicHoliday()).IsPublicHoliday(_fromdate)));
                     if (employeeLeaveUI.AM == true && employeeLeaveUI.ToAM == true && b == false)
                     {
                         employeeLeave.AM = true;
                         employeeLeave.PM = false;
                     }
                     else if (employeeLeaveUI.PM == true && employeeLeaveUI.ToPM == true && b == false)
                     {
                         employeeLeave.AM = false;
                         employeeLeave.PM = true;
                     }
                     else if (employeeLeaveUI.AM == true && employeeLeaveUI.ToPM == true && b == false)
                     {
                         employeeLeave.AM = true;
                         employeeLeave.PM = true;
                     }
                     else if (employeeLeaveUI.PM == true && employeeLeaveUI.ToAM == true && b == false)
                     {
                         employeeLeave.AM = true;
                         employeeLeave.PM = true;
                     }
                     employeeLeave.LeaveDate = _fromdate;
                     businessEmployeeLeave.AddEmployeeLeave(employeeLeave);
                 }


             }
    
             */




        #endregion
        public void AddEmployeeLeave(IEmployeeLeave employeeLeave)
        {
            _dalEmployeeLeave.AddEmployeeLeave(employeeLeave);
        }

        public IList<IEmployeeLeave> GetEmployeeLeaves(int empId)
        {
            return _dalEmployeeLeave.GetEmployeeLeaves(empId);
        }
        public bool DeleteEmployeeLeave(int empId)
        {
            return _dalEmployeeLeave.DeleteEmployeeLeave(empId);
        }

        public IList<IEmployeeLeave> GetEmployeeLeavesForTeam(int teamId)
        {
            return _dalEmployeeLeave.GetEmployeeLeavesForTeam(teamId);
        }
        public IList<IEmployeeLeave> GetEmployeeLeavesForTeam(int teamId, DateTime fromDate, DateTime toDate)
        {
            return _dalEmployeeLeave.GetEmployeeLeavesForTeam(teamId, fromDate, toDate);
        }
    }

}

